package com.smm.web.vmc.controller.admin;

import com.smm.web.vmc.domain.Pager;
import com.smm.web.vmc.domain.Warehouse;
import com.smm.web.vmc.domain.WarehouseImage;
import com.smm.web.vmc.dto.JsonReturn;
import com.smm.web.vmc.dto.WarehouseDto;
import com.smm.web.vmc.service.WarehouseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by zhenghao on 2016/9/6.
 *
 */
@Slf4j
@Controller
@RequestMapping("/admin/warehouse")
public class WarehouseController {

    @Resource
    private WarehouseService warehouseService;


    //按仓库名称搜索
    @RequestMapping("/search.do")
    public ModelAndView search(HttpServletRequest request,String name){

        int pageNo = 1;

        if(name==null) name="";

        if(request.getParameter("page")!=null){

            pageNo = Integer.parseInt(request.getParameter("page"));
        }

        //获取所有仓库列表
        Pager pager = new Pager(pageNo);

        List<WarehouseDto> warehouseList = warehouseService.searchByNameForDtoWithPage(pager,name);

        ModelAndView mv = new ModelAndView("admin/warehouse/search");

        mv.addObject("warehouseList",warehouseList);
        mv.addObject("pager",pager);
        mv.addObject("name",name);

        return mv;

    }


    @RequestMapping("/list.do")
    public ModelAndView list(HttpServletRequest request){

        int pageNo = 1;

        if(request.getParameter("page")!=null){

            pageNo = Integer.parseInt(request.getParameter("page"));
        }

        Pager pager = new Pager(pageNo);


        List<WarehouseDto> warehouseList = warehouseService.getAllForDtoWithPage(pager);

        ModelAndView mv = new ModelAndView("admin/warehouse/list");

        mv.addObject("warehouseList",warehouseList);
        mv.addObject("pager",pager);

        return mv;
    }


    @RequestMapping("/add.do")
    public ModelAndView add(){

        return new ModelAndView("admin/warehouse/add");
    }


    @RequestMapping("/submitAdd.do")
    @ResponseBody
    public JsonReturn submitAdd(Warehouse warehouse,String imageList){

        try{

            log.info("==========imageList:"+imageList);

            warehouseService.add(warehouse,imageList);

            return new JsonReturn(1,"新增成功");

        }catch (Exception e){

            e.printStackTrace();

            return new JsonReturn(0,e.getMessage());
        }

    }


    @RequestMapping("/update.do")
    public ModelAndView update(int id){

        //获取仓库详情
        Warehouse warehouse = warehouseService.getOneById(id);

        //获取所有仓库图片
        List<WarehouseImage> warehouseImages = this.warehouseService.getImageListByid(warehouse.getId());

        ModelAndView mv = new ModelAndView("admin/warehouse/update");

        mv.addObject("warehouse",warehouse);
        mv.addObject("images",warehouseImages);

        return mv;
    }


    @RequestMapping("/submitUpdate.do")
    @ResponseBody
    public JsonReturn submitUpdate(Warehouse warehouse,String imageList){

        log.info("------------ in Warehouse  submitUpdate");

        try{

            log.info("--------id:"+warehouse.getId());

            warehouseService.update(warehouse,imageList);

            return new JsonReturn(1,"修改成功");

        }catch (Exception e){

            e.printStackTrace();

            return new JsonReturn(0,e.getMessage());
        }

    }


    @RequestMapping("/deleteWh.do")
    @ResponseBody
    public JsonReturn deleteWh(int id){

        log.info("------------ id:"+id);

        try{

            warehouseService.delete(id);

            return new JsonReturn(1,"删除成功");

        }catch (Exception e){

            e.printStackTrace();

            return new JsonReturn(0,e.getMessage());
        }

    }


}
